﻿/***ShoppingCart***/
var isShowPriceDetail = true;
checkout = function () {
    if (KevinShop.Common.ShoppingCart.TotalQty == 0) {
        KevinShop.MessageBox.Alert("购物车至少得有一个商品。");
        return;
    }
    var url = document.forms["toPreCheck"].action;
    refreshTotalAmount();
    KevinShop.Common.Ajax.Post(url,null, function (data) {
        if (!data) {
            return;
        }
        if (!data.Status) {
            KevinShop.MessageBox.Alert(data.ErrorMessage);
            return;
        }
        var checkOutUrl = document.forms["toCheckout"].action;
        window.location = checkOutUrl;
    });
};
//检查是否是COM
CheckCOM = function (pid) {
    if (pid == "COM")
        return true;
    return false;
};
IncreaseToCart = function (obj, pid) {
    if (CheckCOM(pid)) {
        return;
    }
    var qtyText = $("#Qty_" + pid);

    var qty = qtyText.text() || qtyText.val();
    var lQty = qtyText.attr("lqty");
    qty++;

    if (!KevinShop.Common.ShoppingCart.CheckQuantity(qty, lQty)) {
        return;
    }

    ChangeToCart(pid, qty);
};
DeclineToCart = function (obj, pid) {
    if (CheckCOM(pid))
        return;

    var qtyText = $("#Qty_" + pid);
    var qty = qtyText.text() || qtyText.val();

    if (!KevinShop.Common.ShoppingCart.CheckQuantity(qty))
        return;
    if (qty == 1) {
        KevinShop.MessageBox.Alert("亲，不能再减啦，点击右边的删除吧。");
        return;
    }
    qty--;
    ChangeToCart(pid, qty);
};
ChangeToCart = function (pid, qty) {
    UpdateCart(pid, qty);
    Refresh(pid, qty);
};
Refresh = function (pid, qty) {
    var uprice = $("#prd_price_" + pid).attr("price");
    var price = !isNaN(uprice) ? parseFloat(uprice) : 0;
    var priceText;
    if (isShowPriceDetail) {
        priceText = qty + " x " + price.toFixed(2) + " = ￥" + (qty * price).toFixed(2);
    }
    else {
        priceText = "￥" + (qty * price).toFixed(2);
    }

    $("#prd_price_" + pid).text(priceText);
    if ($("#Qty_" + pid).text()) {
        $("#Qty_" + pid).text(qty);
    }
    else {
        $("#Qty_" + pid).val(qty);
    }

    $("#headershoppingcart #num").text(KevinShop.Common.ShoppingCart.TotalQty);
};
UpdateCart = function (pid, qty) {
    KevinShop.Common.ShoppingCart.UpdateToCartForQty(pid, qty);
};

UpdateProductNumber = function(pid) {
    if (CheckCOM(pid)) {
        return;
    }
    var qtyText = $("#Qty_" + pid);

    var qty = qtyText.text() || qtyText.val();
    var lQty = qtyText.attr("lqty");

    if (qty <= 0) {
        UpdateCart(pid, 1);
        Refresh(pid, 1);
        return;
    }

    if (!KevinShop.Common.ShoppingCart.CheckQuantity(qty, lQty)) {
        UpdateCart(pid, lQty);
        Refresh(pid, lQty);
        return;
    }

    UpdateCart(pid, qty);
    Refresh(pid, qty);

};
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
    // 通过下面这个API隐藏右上角按钮
    WeixinJSBridge.call('hideOptionMenu');
});

